import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    valid:'',
    account:'15525654444',
    password:'123456'
  },
  rules: {
    account: [
      { required: true, message: '请填写账号!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查账号是否正确!' },
    ],
    password: [
      { required: true, message: '请填写短信验证码!' },
      { pattern: /^\d{6}$/, message: '请检查密码是否正确!' }
    ]
  },
  mobileqq(){
    const { valid, message } = this.validate('account')
    if(!valid){
      wx.$toast(message)
      return
    }  
  },
  codeqq(){
    const { valid, message } = this.validate('password')
    if(!valid){
      console.log(valid)
      wx.$toast(message)
      return
    }  
  },

  async submitForm(){
    const isAllValid = this.validate()
    if(!isAllValid)
    return
    // 2.调用接口做注册/登录
    const {account,password} = this.data
    const {token,refreshToken} = await wx.$http.post('/driver/login/account', { account, password })
    console.log(res)
    //  3.拿到了token,保存到本地
    const app = getApp()
    app.setToken('token',token)
    app.setToken('refreshToken', refreshToken)
     //  4.页面跳转
  const url = this.redirectURL || '/pages/mine/mine'
  wx.redirectTo({
    url,
  })
  }
})